package com.nuoye.camera2studydemo.fragment;

import static android.hardware.camera2.CameraMetadata.COLOR_CORRECTION_ABERRATION_MODE_FAST;
import static android.hardware.camera2.CameraMetadata.COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY;
import static android.hardware.camera2.CameraMetadata.COLOR_CORRECTION_ABERRATION_MODE_OFF;
import static android.hardware.camera2.CameraMetadata.COLOR_CORRECTION_MODE_FAST;
import static android.hardware.camera2.CameraMetadata.COLOR_CORRECTION_MODE_HIGH_QUALITY;
import static android.hardware.camera2.CameraMetadata.COLOR_CORRECTION_MODE_TRANSFORM_MATRIX;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_ANTIBANDING_MODE_50HZ;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_ANTIBANDING_MODE_60HZ;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_ANTIBANDING_MODE_AUTO;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_ANTIBANDING_MODE_OFF;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_MODE_OFF;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_MODE_ON;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_MODE_ON_ALWAYS_FLASH;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_MODE_ON_AUTO_FLASH;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_MODE_ON_EXTERNAL_FLASH;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_AUTO;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_VIDEO;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_EDOF;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_MACRO;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_OFF;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_TRIGGER_CANCEL;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_TRIGGER_IDLE;
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_TRIGGER_START;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_AUTO;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_CLOUDY_DAYLIGHT;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_DAYLIGHT;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_FLUORESCENT;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_INCANDESCENT;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_OFF;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_SHADE;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_TWILIGHT;
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_WARM_FLUORESCENT;
import static android.hardware.camera2.CameraMetadata.FLASH_MODE_OFF;
import static android.hardware.camera2.CameraMetadata.FLASH_MODE_SINGLE;
import static android.hardware.camera2.CameraMetadata.FLASH_MODE_TORCH;
import static android.hardware.camera2.CaptureRequest.FLASH_MODE;

import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CaptureRequest;

import androidx.core.util.Consumer;

import com.nuoye.camera2studydemo.ChickConsumer;
import com.nuoye.camera2studydemo.base.Camera2ControlFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 预览。
 */
public class CameraPreviewFragment extends Camera2ControlFragment {
    @Override
    protected List<ChickConsumer> getControls() {
        List<ChickConsumer> chickConsumers = new ArrayList<>();
        chickConsumers.add(new ChickConsumer("开启闪光灯", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(FLASH_MODE, FLASH_MODE_TORCH);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("关闭闪光灯", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(FLASH_MODE, FLASH_MODE_OFF);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("闪光灯拍照，开起来好像没有啥效果", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(FLASH_MODE, FLASH_MODE_SINGLE);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("色彩矫正模式：TRANSFORM_MATRIX", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.COLOR_CORRECTION_MODE, COLOR_CORRECTION_MODE_TRANSFORM_MATRIX);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("色彩矫正模式：FAST", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.COLOR_CORRECTION_MODE, COLOR_CORRECTION_MODE_FAST);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("色彩矫正模式：HIGH_QUALITY", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.COLOR_CORRECTION_MODE, COLOR_CORRECTION_MODE_HIGH_QUALITY);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("色彩校正像差模式：OFF", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.COLOR_CORRECTION_ABERRATION_MODE, COLOR_CORRECTION_ABERRATION_MODE_OFF);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("色彩校正像差模式：FAST", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.COLOR_CORRECTION_ABERRATION_MODE, COLOR_CORRECTION_ABERRATION_MODE_FAST);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("色彩校正像差模式：HIGH_QUALITY", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.COLOR_CORRECTION_ABERRATION_MODE, COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("HZ：off", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_ANTIBANDING_MODE, CONTROL_AE_ANTIBANDING_MODE_OFF);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("HZ：50hz", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_ANTIBANDING_MODE, CONTROL_AE_ANTIBANDING_MODE_50HZ);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("HZ：60hz", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_ANTIBANDING_MODE, CONTROL_AE_ANTIBANDING_MODE_60HZ);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("HZ：auto", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_ANTIBANDING_MODE, CONTROL_AE_ANTIBANDING_MODE_AUTO);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("曝光：off", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_MODE, CONTROL_AE_MODE_OFF);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("曝光：on", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_MODE, CONTROL_AE_MODE_ON);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("曝光：AUTO_FLASH", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_MODE, CONTROL_AE_MODE_ON_AUTO_FLASH);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("曝光：ALWAYS_FLASH", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_MODE, CONTROL_AE_MODE_ON_ALWAYS_FLASH);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("曝光：AUTO_FLASH_REDEYE", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_MODE, CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("曝光：闪光灯", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_MODE, CONTROL_AE_MODE_ON_EXTERNAL_FLASH);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("捕获测光：IDLE", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CONTROL_AE_PRECAPTURE_TRIGGER_IDLE);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("捕获测光：START", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CONTROL_AE_PRECAPTURE_TRIGGER_START);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("捕获测光：取消", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动对焦：off", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_MODE, CONTROL_AF_MODE_OFF);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动对焦：自动", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_MODE, CONTROL_AF_MODE_AUTO);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动对焦：MACRO", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_MODE, CONTROL_AF_MODE_MACRO);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动对焦：连续视频", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_MODE, CONTROL_AF_MODE_CONTINUOUS_VIDEO);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动对焦：连续图片", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_MODE, CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动对焦：数字对焦", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_MODE, CONTROL_AF_MODE_EDOF);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("触发控制：IDLE", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CONTROL_AF_TRIGGER_IDLE);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("触发控制：开始", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CONTROL_AF_TRIGGER_START);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("触发控制：取消", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CONTROL_AF_TRIGGER_CANCEL);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：off", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_OFF);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：auto", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_AUTO);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：白炽灯", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_INCANDESCENT);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：荧光", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_FLUORESCENT);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：暖荧光", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_WARM_FLUORESCENT);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：日光", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_DAYLIGHT);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：多云的日光", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_CLOUDY_DAYLIGHT);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：黄昏", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_TWILIGHT);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        chickConsumers.add(new ChickConsumer("自动白平衡：阴影", new Consumer<String>() {
            @Override
            public void accept(String s) {
                builder.set(CaptureRequest.CONTROL_AWB_MODE, CONTROL_AWB_MODE_SHADE);
                try {
                    mSession.setRepeatingRequest(builder.build(), null, cameraHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        return chickConsumers;
    }
}
